package com.example.springbatchdemo.config.file;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.validation.BindException;

/**
 * @Classname FileItemDemo
 * @Description TODO
 * @Date 2020/9/22 21:37
 * @Created by kevin
 */
@Configuration
public class FileItemDemo {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Autowired
    private FlatFileWriter flatFileWriter;

    @Bean
    public Job fileItemDemoJob() {

        return jobBuilderFactory.get("fileItemDemoJob")
                .start(fileItemDemoStep())
                .build();
    }

    @Bean
    public Step fileItemDemoStep() {

        return stepBuilderFactory.get("fileItemDemoStep")
                .<Weather, Weather>chunk(20)
                .reader(flatFileReaer())
                .writer(flatFileWriter)
                .build();
    }

    @Bean
    @StepScope
    public FlatFileItemReader<Weather> flatFileReaer() {
        FlatFileItemReader<Weather> reader = new FlatFileItemReader<>();
        // 指定读取文件的位置
        reader.setResource(new ClassPathResource("data.csv"));
        // 跳过第一行
        reader.setLinesToSkip(1);
        // // 创建LineTokenizer接口实现，将字符串转换为FieldSet
        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
        // // 设定每个字段的名称，如果不设置需要使用索引获取值
        tokenizer.setNames(new String[]{"id", "month", "type", "value"});
        // 初始化 LineMapper实现类
        DefaultLineMapper<Weather> mapper = new DefaultLineMapper<>();
        // 设置tokenizer工具
        mapper.setLineTokenizer(tokenizer);
        //定义FieldSetMapper用于FieldSet->Weather
        mapper.setFieldSetMapper(new FieldSetMapper<Weather>() {
            @Override
            public Weather mapFieldSet( FieldSet fieldSet ) throws BindException {
                Weather weather = new Weather();
                weather.setId(fieldSet.readString("id"));
                weather.setMonth(fieldSet.readString("month"));
                weather.setType(fieldSet.readString("type"));
                weather.setValue(fieldSet.readString("value"));
                return weather;
            }
        });
        mapper.afterPropertiesSet();
        reader.setLineMapper(mapper);
        reader.open(new ExecutionContext());
        return reader;
    }
}
